﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;

namespace MvcApplication1.Controllers
{
    public class DanhGiaVaPhanHoiController : Controller
    {
        //
        // GET: /DanhGiaVaPhanHoi/

        public ActionResult DanhGia()
        {
            if (Session["tendangnhap"] == null || Session["tendangnhap"].ToString() == "")
            {
                return RedirectToAction("DangNhap", "TaiKhoan");
            }
            DataClassesDataContext dataContext = new DataClassesDataContext();
            string tenDangNhap = Session["tendangnhap"].ToString();
            int KhachHang_Id = (from user in dataContext.KhachHangs where user.UserName.Equals(tenDangNhap) select user.Id).FirstOrDefault();
            var dsGopY_Phanhois = from gopY_PhanHoi in dataContext.GopY_PhanHois where gopY_PhanHoi.KhachHang_Id == KhachHang_Id select gopY_PhanHoi;
            if (dsGopY_Phanhois == null)
            {
                ViewBag.DangKySuaChuaEmpty = "Không tìm thấy kết quả nào";
            }
            List<GopY_PhanhoiModel> dsGopY_PhanhoiModel = new List<GopY_PhanhoiModel>();
            foreach (GopY_PhanHoi gopY_Phanhoi in dsGopY_Phanhois)
            {
                GopY_PhanhoiModel gopY_PhanhoiModel = new GopY_PhanhoiModel();
                gopY_PhanhoiModel.noiDungGopY = gopY_Phanhoi.NoiDung;
                gopY_PhanhoiModel.phanHoi = gopY_Phanhoi.PhanHoi;
                dsGopY_PhanhoiModel.Add(gopY_PhanhoiModel);
            }
            ViewBag.DanhSachGopYPhanHoi = dsGopY_PhanhoiModel;
            ViewBag.MenuClass = Session["MenuClass"];
            return View();
        }

        [HttpPost]
        public ActionResult DanhGia(GopY_PhanhoiModel model)
        {
            DataClassesDataContext dataContext = new DataClassesDataContext();
            GopY_PhanHoi gopY = new GopY_PhanHoi();
            string userName = Session["tendangnhap"].ToString();
            int userID = (from user in dataContext.KhachHangs where user.UserName.Equals(userName) select user).FirstOrDefault().Id;
            gopY.NoiDung = model.noiDungGopY;
            gopY.PhanHoi = "";
            gopY.KhachHang_Id = userID;
            dataContext.GopY_PhanHois.InsertOnSubmit(gopY);
            dataContext.SubmitChanges();

            string tenDangNhap = Session["tendangnhap"].ToString();
            int KhachHang_Id = (from user in dataContext.KhachHangs where user.UserName.Equals(tenDangNhap) select user.Id).FirstOrDefault();
            var dsGopY_Phanhois = from gopY_PhanHoi in dataContext.GopY_PhanHois where gopY_PhanHoi.KhachHang_Id == KhachHang_Id select gopY_PhanHoi;
            List<GopY_PhanhoiModel> dsGopY_PhanhoiModel = new List<GopY_PhanhoiModel>();
            foreach (GopY_PhanHoi gopY_Phanhoi in dsGopY_Phanhois)
            {
                GopY_PhanhoiModel gopY_PhanhoiModel = new GopY_PhanhoiModel();
                gopY_PhanhoiModel.noiDungGopY = gopY_Phanhoi.NoiDung;
                gopY_PhanhoiModel.phanHoi = gopY_Phanhoi.PhanHoi;
                dsGopY_PhanhoiModel.Add(gopY_PhanhoiModel);
            }
            ViewBag.DanhSachGopYPhanHoi = dsGopY_PhanhoiModel;
            ViewBag.MenuClass = Session["MenuClass"];
            return View("DanhGia");
        }

    }
}
